﻿using System;
using MetroContractSample.Data;
using Windows.ApplicationModel.Activation;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Navigation;

// The Blank Page item template is documented at http://go.microsoft.com/fwlink/?LinkId=234238

namespace MetroContractSample
{
  /// <summary>
  /// An empty page that can be used on its own or navigated to within a Frame.
  /// </summary>
  public sealed partial class BlankPage : Page
  {
    public BlankPage()
    {
      this.InitializeComponent();

      this.Loaded += BlankPage_Loaded;
    }

    void BlankPage_Loaded(object sender, RoutedEventArgs e)
    {
      MoveMainPage();
    }


    private Type nextPage;
    private IActivatedEventArgs args;

    /// <summary>
    /// Invoked when this page is about to be displayed in a Frame.
    /// </summary>
    /// <param name="e">Event data that describes how this page was reached.  The Parameter
    /// property is typically used to configure the page.</param>
    protected override void OnNavigatedTo(NavigationEventArgs e)
    {
      var param = e.Parameter as Tuple<Type, IActivatedEventArgs>;
      this.nextPage = param.Item1;
      this.args = param.Item2;
    }



    private async void MoveMainPage()
    {
      if (!App.DataSource.HasData)
        await (new MyDataLoader()).GatherUserData(App.DataSource);

      if (args.Kind == ActivationKind.Search)
      {
        string queryText = (args as SearchActivatedEventArgs).QueryText;
        SearchResultsPage.Activate(queryText);
        return;
      }

      if (args.Kind == ActivationKind.FileOpenPicker)
      {
        var fileOpenPickerPage = new MetroContractSample.FileOpenPickerPage();
        fileOpenPickerPage.Activate(this.args as FileOpenPickerActivatedEventArgs);
        return;
      }

      var rootFrame = new Frame();
      rootFrame.Navigate(this.nextPage);

      // Place the frame in the current Window and ensure that it is active
      Window.Current.Content = rootFrame;
      Window.Current.Activate();
    }

  }
}
